<script type="text/javascript">
    $(function () {

    });

</script>
<div class="row">
    <div class="col-xs-12">
        <div class="box box-default">
            <div class="box-header">
                <div class="alert alert-success alert-dismissable fade in hide" role="alert" id="addSuccess">
                    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                    <p><i class="icon fa fa-check"></i> <strong>恭喜！</strong>邮件模板添加成功</p>
                </div>
                <p class="myInfoBox bg-warning text-warning"><i class="icon fa fa-warning"></i>
                    信息填写完整无误才可提交，参数设置：1、regSuccess : 注册成功邮件模板 2、findPsd ：密码找回邮件模板 3、euserName,elink为变量参数 4、链接请求接口：/users/reSetPsd/elink
                </p>
            </div>
            <div class="box-body">
                <form role="form" class="form-horizontal" name="myForm" ng-submit="processForm(myForm.$valid)" novalidate>
                    <div class="form-group">
                        <label class="control-label col-sm-3">邮件模板标题</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control input-sm" name="title" ng-minlength="5" ng-maxlength="30" ng-model="formData.title" required/>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.title.$invalid && !myForm.title.$pristine"><i class="fa fa-times-circle-o"></i> 5-30个非特殊字符</label>

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">邮件概要</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control input-sm" name="subject" ng-minlength="5" ng-maxlength="30" ng-model="formData.subject" required/>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.subject.$invalid && !myForm.subject.$pristine"><i class="fa fa-times-circle-o"></i> 5-30个非特殊字符</label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-sm-3">模板类别</label>
                        <div class="col-sm-2">
                            <select name="" id="" class="form-control" ng-model="formData.type" required>
                                <option value="regSuccess">注册成功</option>
                                <option value="findPsd">密码找回</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">邮件详情</label>
                        <div class="col-sm-6">
                            <textarea class="" name="comments" ng-model="formData.comments" id="container" ng-minlength="50" placeholder="请输入邮件详情" ueditor>{{formData.comments}}</textarea>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.comments.$invalid && !myForm.comments.$pristine"><i class="fa fa-times-circle-o"></i>内容不得少于50个字</label>

                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" ng-disabled="myForm.$invalid">提交</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>

<script>

    angular.module("adminApp",[])
            .controller("adminCtrl",function($scope,$http){
                $scope.formData = {};

                $scope.targetID = window.location.href.split("/")[7];
                if($scope.targetID){
                    $http.get("/admin/manage/emailTemp/item?uid="+$scope.targetID).success(function(result){

                        $scope.formData = result;

                    })
                }
                // 添加或修改邮件模板
                $scope.processForm = function(isValid){
                    var url = "/admin/manage/emailTemp/addEmailTemp";
                    if($scope.targetID){
                        url = "/admin/manage/emailTemp/modify?uid="+$scope.targetID;
                    }
                    if(isValid){
                        $http({
                            method  : 'POST',
                            url     : url,
                            data    : $.param($scope.formData),  // pass in data as strings
                            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
                        })
                                .success(function(data) {
                                    if(data === "success"){
                                       window.location = "/admin/manage/emailTempList";
                                    }else{
                                        alert("未知异常，请稍后重试");
                                    }
                                });
                    }
                    else{
                        alert("校验失败，请检查");
                    }
                }
            })
            .directive('ueditor', function ($timeout) { //angular绑定ueditor
                return {
                    restrict: 'A',
                    require: 'ngModel',
                    link: function (scope, element, attrs, ctrl) {
                        var id = 'ueditor_' + Date.now();
                        element[0].id = id;
                        var ue = UE.getEditor(id, {
                            initialFrameWidth: '100%',
                            initialFrameHeight: '500',
                            autoHeightEnabled: true
                        });
                        ue.ready(function () {
                            ue.addListener('contentChange', function () {
                                ctrl.$setViewValue(ue.getContent());
                                if (!scope.$$phase) {
                                    scope.$apply();
                                }
                            });
                        });
                    }
                };
            });


</script>